﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SEO.Model;
using System.Data.SQLite;

namespace SEO.DAL
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 关键词表
    /// 完成日期：2011年6月1日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class KeywordsDAL
    {

        /// <summary>
        /// 获取Keywords表中数据
        /// <returns>返回List</returns>
        /// </summary>
        public List<Keywords> GetAllKeywordsDAL()
        {
            List<Keywords> list = new List<Keywords>();
            string strSql = "select * from Keywords";
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.DBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    Keywords ks = new Keywords();
                    ks.KeyID = int.Parse(dr["key_id"].ToString());
                    ks.KeyName = dr["key_name"].ToString();
                    list.Add(ks);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定Keywords -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 根据指定SQL语句获取Keywords表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回List</returns>
        public List<Keywords> GetKeywordsByWhereDAL(string strSql)
        {
            List<Keywords> list = new List<Keywords>();
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.DBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    Keywords ks = new Keywords();
                    ks.KeyID = int.Parse(dr["key_id"].ToString());
                    ks.KeyName = dr["key_name"].ToString();
                    list.Add(ks);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据指定SQL语句获取Keywords表中数据 -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 往Keywords表中插入数据
        /// <param name="ks">实例化Keywords类</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int InsertKeywordsDAL(SEO.Model.Keywords ks)
        {
            int iNum = 0;
            string strSql = "insert into Keywords(key_id,key_name) " +
             " values(" + ks.KeyID + ",'" + ks.KeyName + "')";
            try
            {
                iNum = SEO.DBSqlHelp.DBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("往Keywords表中插入数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 根据指定条件删除Keywords表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int DeleteKeywordsDAL(string strSql)
        {
            int iNum = 0;
            try
            {
                iNum = SEO.DBSqlHelp.DBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据指定条件删除Keywords表中数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 更新Keywords表中数据
        /// </summary>
        /// <param name="ks">实例化Keywords类</param>
        /// <returns>返回影响行数</returns>
        public int UpdateKeywordsDAL(SEO.Model.Keywords ks)
        {
            int iNum = 0;
            string strSql = "update Keywords set key_name=" + ks.KeyName + " where key_id=" + ks.KeyID;
            try
            {
                iNum = SEO.DBSqlHelp.DBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("更新Keywords表中数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 绑定表Keywords到DataGridView
        /// <param name="dgv">DataGridView控件名称</param>
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        public void BindKeywordsDAL(System.Windows.Forms.DataGridView dgv, string strSql)
        {
            try
            {
                //清除最后自动生成的行
                dgv.AutoGenerateColumns = false;
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.DBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataAdapter da = new SQLiteDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds, "Keywords");
                ds.Tables["Keywords"].Columns.Add("ViewCatch", typeof(string));
                for (int i = 0; i < ds.Tables["Keywords"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["Keywords"].Rows[i];
                }
                dgv.DataSource = ds.Tables["Keywords"].DefaultView;
                da.Dispose();
                cn.Close();
                //清除当前选择行行为
                dgv.CurrentCell = null;

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定Keywords -> " + ex.Message);
            }
        }

    }
}
